package org.example.authservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.authservice.mapper.PermissionMapper;
import org.example.authservice.pojo.MakeMenuTree;
import org.example.authservice.pojo.Permission;
import org.example.authservice.service.PermissionService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 顾翔宇
 * @since 2024/12/16 11：34
 * @version 1.0
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService{
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 根据权限id查询权限对象
     *
     * @param permissionId 权限对象Id
     * @return 新增的权限id
     */
    @Override
    public Permission selectById(Integer permissionId) {
        return permissionMapper.selectById(permissionId);
    }

    /**
     * 根据父节点获取子节点列表
     *
     * @return 权限列表
     */
    @Override
    public List<Permission> getParent() {
        String[] type = {"0","1"};
        List<String>  strings =Arrays.asList(type);
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().in(Permission::getType,strings).orderByAsc(Permission::getSort);
        List<Permission> permissionList= this.baseMapper.selectList(query);
        //组装顶级树
        Permission permission = new Permission();
        permission.setComment("顶级菜单");
        permission.setLabel("顶级菜单");
        permission.setParentId(-1);
        permission.setId(0);
        permission.setValue(0);
        permissionList.add(permission);
        //组装菜单树
        return MakeMenuTree.makeTree(permissionList, -1);
    }

    /**
     * 根据角色id获取权限列表
     *
     * @param roleId 角色id
     * @return 权限列表
     */
    @Override
    public List<Permission> getPermissionByRoleId(Integer roleId) {
        return this.baseMapper.getPermissionByRoleId(roleId);
    }

}